/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.corespring.restaurant.dao.hibernate;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.corespring.restaurant.dao.RecetaDao;
import org.corespring.restaurant.model.Receta;

/**
 *
 * @author u201014509
 */
@Repository
public class RecetaDaoHibernate extends HibernateDaoSupport implements RecetaDao {

    @Autowired
    public RecetaDaoHibernate(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public List<Receta> obtenerRecetas() {
        return getHibernateTemplate().find(" from Receta");
    }

    @Override
    public List<Receta> obtenerPlatoReceta(String sIdplato) {
        return getHibernateTemplate().find(" from Receta where idplato='" + sIdplato + "'");
    }

    @Override
    public List<Receta> obtenerListaIngredientesPorPlato(String idPlato) {
        return getHibernateTemplate().find(" from Receta where idplato='" + idPlato + "'");
    }

    @Override
    public void guardarReceta(Receta receta) {
      getHibernateTemplate().saveOrUpdate(receta);
      getHibernateTemplate().flush();
    }

    @Override
    public Receta obtenerRecetaByPlatoAndIngrediente(String idPlato,String sIding) {
        List<Receta> list = getHibernateTemplate().find(" from Receta where idplato='" + idPlato + "' and iding = '" + sIding + "' ");
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public void EliminarReceta(String sIdplato, String sIding) {
        Receta receta = obtenerRecetaByPlatoAndIngrediente(sIdplato, sIding);
        getHibernateTemplate().delete(receta);
    }
}
